package com.example.administrator.holiday;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.ArrayList;

/**
 * Created by Administrator on 2014/12/16.
 */
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    ArrayList<AppInfo> appList;
    // Provide a reference to the views for each data item
    // Complex data items may need more than one view per item, and
    // you provide access to all the views for a data item in a view holder
    public static class ViewHolder extends RecyclerView.ViewHolder {
        // each data item is just a string in this case
        public TextView appName;
        public ImageView image;
        public TextView packageName;
        public ViewHolder(View v) {
            super(v);
            appName = (TextView)v.findViewById(R.id.item_name);
            packageName = (TextView)v.findViewById(R.id.item_packagename);
            image = (ImageView)v.findViewById(R.id.item_image);
        }
    }
    // Provide a suitable constructor (depends on the kind of dataset)
    public MyAdapter( ArrayList<AppInfo> appList) {
        this.appList = appList;
    }
    // Create new views (invoked by the layout manager)
    @Override
    public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent,
                                                   int viewType) {
        // create a new view
        View v = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.item_text, parent, false);
        // set the view's size, margins, paddings and layout parameters
        ViewHolder vh = new ViewHolder(v);
        return vh;
    }
    // Replace the contents of a view (invoked by the layout manager)
    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        // - get element from your dataset at this position
        // - replace the contents of the view with that element
        holder.appName.setText(appList.get(position).appName);
        holder.packageName.setText(appList.get(position).packageName);
        holder.image.setBackground(appList.get(position).appIcon);
    }
    // Return the size of your dataset (invoked by the layout manager)
    @Override
    public int getItemCount() {
        return appList.size();
    }
}
